package com.zzl.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * Title: ListValidConstraintValidator.class<br>
 * Description: <br>
 * Copyright (c)  2021    <br>
 * Create DateTime: 2021/10/13 18:22 <br>
 *
 * @author zhuangzhuangliang
 */
public class ListValidConstraintValidator implements ConstraintValidator<ListValid,Integer> {

    private Set<Integer> set = new HashSet<>();
    /**
     * 初始化值
     * @param constraintAnnotation
     */
    @Override
    public void initialize(ListValid constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        for (int val : vals) {
            set.add(val);
        }
    }

    /**
     * 校验数据
     * @param value
     * @param context
     * @return
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
